package Utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;


public class Dictionary {

	ArrayList<String> words = new ArrayList<String>();
	Object[] finish;


	public Object[] createDic(String file) throws IOException {

		String line;
		BufferedReader in = new BufferedReader(new FileReader(file));

		while ((line = in.readLine()) != null) {
			String[] strings = line.split(" ");

			for (int i = 0; i < strings.length; i++) {
				String current;
				current = strings[i];
				String result = correct(current);
				result = result.toLowerCase();
				if (result.length() > 1 && !contains(words, result)) {
					words.add(result);
					finish = words.toArray();
					Arrays.sort(finish);
				}
			}
		}

		return finish;
	}

	public String correct(String string) {
		String[] corrected = {"</?\\w++[^>]*+>", "\\W", "\\d", " ","	"};
		String temp = string;
		for (int i = 0; i < corrected.length; i++) {
			temp = temp.replaceAll(corrected[i], "");
//			System.out.println(temp.length() + temp);
		}
		return temp;
	}

	public boolean contains(ArrayList<String> array, String string) {

		for (int i = 0; i < array.size(); i++) {
			if (array.get(i).equals(string)) {
				return true;
			}
		}
		return false;
	}




	public static void main(String[] args) {
		FileWriter file = null;
		try {
			file = new FileWriter("mydictionary.txt");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		BufferedWriter out = new BufferedWriter (file);
		Dictionary dic = new Dictionary();
		Object[] result = null;
		try {
			result = dic.createDic("alldoc.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < result.length; i++) {
			try {
				out.write(result[i].toString() + "\n");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}